/**
 * Created by wzq on 15-3-28.
 */

var express = require('express');
var router = express.Router();
var WeixinApi = require("../weixin/WeixinApi.js");
var weixinObj = new WeixinApi();
var debug = require('debug')('test');
var config = require('../config/config.js')();

module.exports = exports = router;

// get /test
router.get('/', function (req, res, next) {
    //res.status(200).end('test ok\n');
    res.redirect('/html/test.html');
});

// query string /test/qs?a=1&b=2
router.get('/qs', function (req, res, next) {
    var query = require('url').parse(req.url, true).query;
    res.status(200).end(JSON.stringify(query));
});

var bodyParser = require('body-parser');
router.post('/json', function (req, res, next) {
    bodyParser.json()(req, res, function () {
        debug(req.body);
        res.status(200).end('post json ok\n');
    });
});

router.post('/form', function (req, res, next) {
    var mid = bodyParser.urlencoded({
        extended: false
    });
    mid(req, res, function () {
        debug(req.body);
        res.status(200).end('post form ok\n');
    });
});


// get /test/employee
router.get('/employee', function (req, res, next) {
    res.status(200).end('test ok\n');
});

// get /test/test/department
router.get('/test/department', function (req, res, next) {
    res.status(200).end('test ok\n');
});

router.get('/weixin/accesstoken', function (req, res, next) {
    weixinObj.getAccessToken().then(function (data) {
        debug('success', data);
        res.status(200).end(JSON.stringify(data));
    }, function (err) {
        debug('err', err);
        res.status(500).end(err);
    });
});

router.get('/weixin/userinfo/:userid', function (req, res, next) {
    var userid = req.params.userid;
    debug('userid=', userid);
    weixinObj.getUserInfo(userid).then(function (data) {
        debug('success', data);
        res.status(200).end(JSON.stringify(data));
    }, function (err) {
        debug('err', err);
        res.status(500).end(err);
    });
});

router.get('/weixin/alluser', function (req, res, next) {
    weixinObj.getAllUser().then(function (data) {
        debug('success', data);
        res.status(200).end(JSON.stringify(data));
    }, function (err) {
        debug('err', err);
        res.status(500).end(err);
    });
});


router.get('/weixin/send/text', function (req, res, next) {
    weixinObj.postMsg({
        "touser": "@all",
        "msgtype": "text",
        "agentid": config.weixin.agentId,
        "text": {
            "content": "from test"
        }
    }).then(function (data) {
        debug('success', JSON.stringify(data));
        res.status(200).end(JSON.stringify(data));
    }, function (err) {
        debug('err', err);
        res.status(500).end(JSON.stringify(err));
    });
});

router.get('/weixin/send/news', function (req, res, next) {
    weixinObj.postMsg({
        "touser": "@all",
        "msgtype": "news",
        "agentid": config.weixin.agentId,
        "news": {
            "articles": [
                // {
                // 	"title": "Title 1",
                // 	"description": "Description 1",
                // 	"url": "http://news.qq.com/a/20150407/023457.htm",
                // 	"picurl": "http://img1.gtimg.com/news/pics/hv1/234/122/1816/118116744.jpg"
                // },
                {
                    "title": "Title 2",
                    "description": "Description 2",
                    // "url": "http://news.qq.com/a/20150407/000578.htm",
                    "url": config.index,
                    "picurl": "http://img1.gtimg.com/news/pics/hv1/1/67/1816/118102486.jpg"
                }
            ]
        }
    }).then(function (data) {
        debug('success', JSON.stringify(data));
        res.status(200).end(JSON.stringify(data));
    }, function (err) {
        debug('err', err);
        res.status(500).end(JSON.stringify(err));
    });
});